/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase;

import com.facebook.drawee.backends.pipeline.DraweeConfig;
import com.facebook.fresco.samples.showcase.imageformat.KeyframesDecoderExample;
import com.facebook.imagepipeline.decoder.ImageDecoderConfig;
import ohos.app.Context;
import ohos.data.DatabaseHelper;

public class CustomImageFormatConfigurator {

  private static final String IMAGE_FORMAT_PREFS = "fresco_image_format_prefs";
  private static final String IMAGE_FORMAT_COLOR_KEY = "color";
  private static final String IMAGE_FORMAT_SVG_KEY = "svg";

  public static ImageDecoderConfig createImageDecoderConfig(Context context) {
    ImageDecoderConfig.Builder config = ImageDecoderConfig.newBuilder();
    /*
    if (isGlobalColorDecoderEnabled(context)) {
      config.addDecodingCapability(
          ColorImageExample.IMAGE_FORMAT_COLOR,
          ColorImageExample.createFormatChecker(),
          ColorImageExample.createDecoder());
    }
    if (isSvgEnabled(context)) {
      config.addDecodingCapability(
          SvgDecoderExample.SVG_FORMAT,
          new SvgDecoderExample.SvgFormatChecker(),
          new SvgDecoderExample.SvgDecoder());
    }
     */
    if (isKeyframesEnabled()) {
      config.addDecodingCapability(
          KeyframesDecoderExample.IMAGE_FORMAT_KEYFRAMES,
          KeyframesDecoderExample.createFormatChecker(),
          KeyframesDecoderExample.createDecoder());
    }
    return config.build();
  }

  public static void addCustomDrawableFactories(
      Context context, DraweeConfig.Builder draweeConfigBuilder) {
    // We always add the color drawable factory so that it can be used for image decoder overrides,
    // see ImageFormatOverrideExample.
      /*
    draweeConfigBuilder.addCustomDrawableFactory(ColorImageExample.createDrawableFactory());
    if (isSvgEnabled(context)) {
      draweeConfigBuilder.addCustomDrawableFactory(new SvgDecoderExample.SvgDrawableFactory());
    }
       */
    if (isKeyframesEnabled()) {
      draweeConfigBuilder.addCustomDrawableFactory(KeyframesDecoderExample.createDrawableFactory());
    }
  }

  public static boolean isGlobalColorDecoderEnabled(Context context) {
    return getBoolean(context, IMAGE_FORMAT_COLOR_KEY, false);
  }

  public static void setGlobalColorDecoderEnabled(Context context, boolean colorEnabled) {
    setBoolean(context, IMAGE_FORMAT_COLOR_KEY, colorEnabled);
  }

  public static boolean isSvgEnabled(Context context) {
    return getBoolean(context, IMAGE_FORMAT_SVG_KEY, false);
  }

  public static void setSvgEnabled(Context context, boolean svgEnabled) {
    setBoolean(context, IMAGE_FORMAT_SVG_KEY, svgEnabled);
  }

  public static boolean isKeyframesEnabled() {
    return  true;
  }

  private static boolean getBoolean(Context context, String key, boolean defaultValue) {
    return new DatabaseHelper(context)
        .getPreferences(IMAGE_FORMAT_PREFS)
        .getBoolean(key, defaultValue);
  }

  private static void setBoolean(Context context, String key, boolean value) {
      new DatabaseHelper(context).getPreferences(IMAGE_FORMAT_PREFS).putBoolean(key,value).flush();
      /*
    context
        .getSharedPreferences(IMAGE_FORMAT_PREFS, Context.MODE_PRIVATE)
        .edit()
        .putBoolean(key, value)
        .apply();

       */
  }
}